home *** CD-ROM | disk | FTP | other *** search
- Path: news.cityu.edu.hk!95469487
- From: 95469487@cpccux0.cityu.edu.hk ()
- Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.perl
- Subject: Re: Stupid array problems
- Followup-To: comp.lang.c,comp.lang.c++,comp.lang.perl
- Date: 14 Jan 1996 07:53:45 GMT
- Organization: City University of Hong Kong
- Message-ID: <4dacq9$a05@ctylnk.cityu.edu.hk>
- References: <4d9b9v$14n@paperboy.ids.net>
- NNTP-Posting-Host: cpccux1.cityu.edu.hk
- X-Newsreader: TIN [version 1.2 PL2]
-
- scarney (scarney@conan.ids.net) wrote:
- : Ok, I've been having what I thought would be a simple problem but no one
- : seems to quite figure out. I Have an array of strings, both the key and
- : the string being pointers. Putting information onto the array is like a
- : stack. My problem comes in the removal of information from it. Popping
- : from a stack only pulls the lastelement out of the array, I want to pull
- : any element out. Destroying the data isn't the hard part because I just
- : blow up the pointers...the problem is that the indexing for the array
- : gets kind of screwy. If I have an array organized by integers with
- : elements 1 2 3 4 5 6 7 8, if I decided to kill element #4 I'm going to
- : have an array of 1 2 3 5 6 7 8, and as time goes on its going to get more
- : screwy. Is there an easy way to delete an element while preserving some
- : semblance of sequence in the array indexing? In Perl I'd use the splice()
- : command...is there anything similar in C/C++?
-
- : I get the feeling I'm looking at this from a completely wrong
- : perspective, so don't flame me if I am...
-
- : Thanks.
- : -Seth
- I think a better way is to use a "link" rather than array. for example
- :
-
- class node
- {
- friend class array;
- int index;
- int foo; // content
- node *link; // a pointer (important) pointed back to node again
- };
-
- class array
- {
- void add_item(int);
- void delete_item(int); // function you want
- node *head;
- };
-
- void array::delete_item(int index) // input: which item you want to delete
- {
- node *prev,*temp;
- temp = head;
- while (temp)
- { if (temp->index == index) break;
- else {prev=temp; temp=temp->link}}
-
- if (temp != 0)
- { prev->link=temp->link;
- delete temp;}
- }
-
-
-
- certainly, this example is not complete, you can look at your book to check
- it out.
- (0)-->(1)-->(2)-->.........-->(10)-->0
-
-
-
-
-
-